package com.yzy.controller;

import com.yzy.annotation.UserLoginToken;
import com.yzy.entity.Goods;
import com.yzy.entity.Order;
import com.yzy.service.OrderService;
import com.yzy.util.Message;
import com.yzy.util.ResponseCode;
import com.yzy.util.TokenUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @GetMapping("/all")
    public Object all(){
        List<Order> list = orderService.list();
        Map<String,List> map = new HashMap<>();
        map.put("data",list);

        return map;
    }
    @UserLoginToken
    @GetMapping("/search")
    public Object search(){
        String downloadUserId= TokenUtil.getTokenUserId();
        List<Goods> goodsList = orderService.search(Integer.valueOf(downloadUserId));
        Map<String, List<Goods>> map = new HashMap<>();
        map.put("data",goodsList);
        return ResponseCode.buildSuccess(map);
    }

    /**
     * @description 创建订单接口
     * @param goodsId 商品id
     * @return java.lang.Object
     * @author mgl
     * @date 2023/8/12 16:39
     */
    @UserLoginToken
    @GetMapping("/create")
    public Object create(@RequestParam("goodsId") Integer goodsId){
        Integer userId = Integer.valueOf(TokenUtil.getTokenUserId());
        String msg = orderService.createOrder(userId, goodsId);
        if (msg.equals(Message.ORDER_CREATE_SUCCESS))
            return ResponseCode.buildSuccess(msg);
        else return ResponseCode.buildFailed(msg);
    }
}
